package sicau.practice.community.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import sicau.practice.community.dto.Car;
import sicau.practice.community.utils.BaiduAI;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

@Controller
public class UploadController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(HttpServletRequest req, @RequestParam("file") MultipartFile file, Model m) {
        try {
//            String fileName = "car.jpg";//System.currentTimeMillis()+file.getOriginalFilename();
//            String destFileName=req.getServletContext().getRealPath("")+"uploaded"+File.separator+fileName;

            String destFileName = "F:\\\\shixun\\\\pro\\\\community\\\\src\\\\main\\\\resources\\\\static\\\\img\\\\car.jpg";

            File destFile = new File(destFileName);
            destFile.getParentFile().mkdirs();
            file.transferTo(destFile);

            //识别
            Car car = new BaiduAI().getResult();
            String result = car.getName();
            String description = car.getDescription();
            System.out.println("车型："+result);
            System.out.println("介绍："+description);

//            m.addAttribute("fileName","car.jpg");
            m.addAttribute("result",result);
            m.addAttribute("description",description);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        }

        return "elements";
    }
}
